package com.home.moon.chat.controller;

import com.home.moon.chat.dto.UserAddDto;
import com.home.moon.chat.model.Users;
import com.home.moon.chat.service.impl.UserServiceImpl;
import com.home.moon.util.RedisUtil;
import com.home.moon.util.UnionResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author auto
 */
@Api(tags = "登录管理")
@Controller("/login")
public class LoginController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation("注册")
    @PostMapping("/regist")
    @ResponseBody
    public UnionResult regist(@RequestBody UserAddDto dto){
        UnionResult result = new UnionResult();
        userService.addUser(dto);
        result.setCode(200);
        result.setMessage("注册成功");
        return result;
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    @ResponseBody
    public UnionResult login(@RequestBody Users query, HttpServletResponse response){
        return UnionResult.OK(userService.login(query,response));
    }

    @ApiOperation(value = "退出", httpMethod = "POST")
    @RequestMapping(value = "/logout")
    @ResponseBody
    public UnionResult logout(@RequestParam(value = "token") String token) {
        redisUtil.delete(token);
        return UnionResult.OK("退出成功！");
    }

}
